<template>
  <el-dialog :title="title" :visible.sync="visible" v-if="visible">
    <el-form ref="formRef" :model="formData" :rules="rules" label-postion="right" label-width="100px">
      <el-form-item label="广告名称">
        <el-input placeholder="广告名称" v-model="formData.bannerName"></el-input>
      </el-form-item>
      <el-form-item label="广告地址">
        <el-input placeholder="昵称" v-model="formData.bannerHref"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="广告图片" prop="bannerUrl">
        <el-upload :action="uploadUrl" :headers="uploadHeader" :limit="1" :file-list="fileList" list-type="picture-card" ref="uploadImage" :on-exceed="onExceed" :on-success="onSuccess" :before-upload="beforeUpload">
          <i class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img :src="file.url" alt="" class="el-upload-list__item-thumbnail">
            <div class="el-upload-list__item-actions">
          <span @click="showImage(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
              <span @click="removeImage(file)">
            <i class="el-icon-delete"></i>
          </span>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button type="warning" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="查看图片" :visible.sync="imageDialogVisible" v-if="imageDialogVisible" append-to-body>
      <img :src="showImageSrc" alt="" height="100%" width="100%">
    </el-dialog>
  </el-dialog>
</template>

<script>
const formData = {
  bannerName: '',
  bannerHref: '',
  status: 1,
  bannerUrl: '',
  bannerId: ''
}
export default {
  name: 'BannerUpdate',
  data () {
    return {
      title: '添加广告',
      visible: false,
      formData: { ...formData },
      rules: {
        bannerUrl: [{
          required: true,
          message: '请选择图片'
        }]
      },
      uploadUrl: process.env.VUE_APP_API + 'admin/upload',
      uploadHeader: {
        token: ''
      },
      fileList: [],
      showImageSrc: '',
      imageDialogVisible: false
    }
  },
  methods: {
    init (bannerId) {
      this.visible = true
      this.fileList.length = 0
      if (bannerId) {
        this.title = '修改广告'
        this.$http.get(`admin/banner/info/${bannerId}`).then(data => {
          this.formData = { ...data.data }
          this.fileList.push({ url: process.env.VUE_APP_API + 'img/' + data.data.bannerUrl })
        })
      } else {
        this.title = '添加广告'
        this.formData = { ...formData }
      }
    },
    onExceed () {
      this.$message.error('请先删除原图片')
    },
    onSuccess (res) {
      if (res.code === 200) {
        this.formData.bannerUrl = res.fileName
      } else {
        this.$message.error(res.msg)
      }
    },
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif'
      if (!isImage) {
        this.$message.error('请选择正确的文件格式')
      }
      const isLt20M = (file.size / 1024 / 1024) < 20
      if (!isLt20M) {
        this.$message.error('文件大小不能超过20MB')
      }
      return isImage && isLt20M
    },
    removeImage (file) {
      this.$confirm('确定删除该图片吗？').then(() => {
        this.$refs.uploadImage.handleRemove(file)
        this.formData.bannerUrl = ''
      })
    },
    showImage (file) {
      this.imageDialogVisible = true
      this.showImageSrc = file.url
    },
    saveOrUpdate () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http.post('admin/banner/update', this.formData).then(() => {
            this.$alert(`${this.formData.bannerId ? '修改' : '添加'}成功！`, { type: 'success' }).then(() => {
              this.visible = false
              this.$emit('closeWin')
            })
          })
        }
      })
    }
  },
  created () {
    this.uploadHeader.token = this.$cookies.get('token')
  }
}
</script>

<style scoped>

</style>
